﻿using System;
using AopAlliance.Intercept;
using Landpy.CruiseControlNet.Infrastructure.Aop.WrapAttribute;

namespace Landpy.CruiseControlNet.Infrastructure.Aop.Interceptor
{
    /// <summary>
    /// Log Exception Interceptor,associate with the attriubte：
    /// <see cref="NotThrowExceptionAttribute"/>
    /// <example> The code example is as following:
    /// <code>
    /// class ThrowExceptionAopTest : IAopTest
    /// {
    ///     [LogException]
    ///     [ExceptionMessageBox]
    ///     [NotThrowException]
    ///     public bool TestBoolean()
    ///     {
    ///         throw new NotImplementedException("The ThrowExceptionAopTest TestBoolean Method");
    ///     }
    /// }
    /// </code>
    /// </example>
    /// </summary>
    public class NotThrowExceptionInterceptor : IMethodInterceptor
    {
        /// <summary>
        /// The method DI logic.
        /// </summary>
        /// <param name="invocation">The original method context.</param>
        /// <returns>The original method return value.</returns>
        public object Invoke(IMethodInvocation invocation)
        {
            object returnValue = null;
            try
            {
                returnValue = invocation.Proceed();
            }
            catch
            {
                Type returnType = invocation.Method.ReturnType;
                if (returnType.IsValueType)
                {
                    if (returnType.FullName != null &&
                        returnType.FullName.Equals("System.Boolean", StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = false;
                    }
                    else if (returnType.IsEnum)
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Int16", StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.UInt16", StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Int32", StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.UInt32",
                                                        StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Int64",
                                                        StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.UInt64",
                                                        StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Single",
                                                        StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = 0f;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Double",
                                                        StringComparison.
                                                            CurrentCultureIgnoreCase))
                    {
                        returnValue = 0d;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Decimal",
                                                        StringComparison.
                                                            CurrentCultureIgnoreCase))
                    {
                        returnValue = 0m;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Char",
                                                        StringComparison.
                                                            CurrentCultureIgnoreCase))
                    {
                        returnValue = Char.MinValue;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.SByte",
                                                        StringComparison.
                                                            CurrentCultureIgnoreCase))
                    {
                        returnValue = SByte.MinValue;
                    }
                    else if (returnType.FullName != null &&
                             returnType.FullName.Equals("System.Byte",
                                                        StringComparison.
                                                            CurrentCultureIgnoreCase))
                    {
                        returnValue = Byte.MinValue;
                    }
                }
            }
            return returnValue;
        }
    }
}